//
//  HomeViewController.m
//  App架构Simple
//
//  Created by 胡锦涛 on 2019/2/16.
//  Copyright © 2019 胡锦涛. All rights reserved.
//

#import "HomeViewController.h"
#import "User.h"
#import "UserPresenter.h"
#import "HKDataSource.h"
#import "TableViewCell.h"

//Cell重用标识
static NSString * reuseId = @"reuseId";

@interface HomeViewController ()<UserViewDelegate>
/** TableView */
@property (nonatomic, strong) UITableView * tableView ;
/** 指示器 */
@property (nonatomic, strong) UIActivityIndicatorView * indicator ;
/** 空白占位 */
@property (nonatomic, strong) UIView * emptyView ;
/** Protocol */
@property (nonatomic, strong) UserPresenter * userPresenter ;
/** 数据源 */
@property (nonatomic, strong) HKDataSource * dataSource ;

@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    [self setupUI];
    
}

- (void)setupUI {
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.dataSource = [[HKDataSource alloc] initWithIdentifier:reuseId configureBlock:^(TableViewCell *cell, User *model, NSIndexPath *indexPath) {
        //cell赋值
        cell.model = model;
    }];
    [self.view addSubview:self.tableView];
    self.tableView.dataSource = self.dataSource;
}

- (void)loadData {
    self.userPresenter = [[UserPresenter alloc] init];
    self.userPresenter.delegate = self;
    [self.userPresenter getUsers];
}

#pragma mark - UserViewDelegate

-(void)startLoading {
    self.indicator.hidden = NO;
    [self.indicator startAnimating];
}

- (void)finishLoading {
    [self.indicator stopAnimating];
    self.indicator.hidden = YES;
}

//用户界面的数据
-(void)setUsers:(NSArray<User *> *)userViewData {
    [self.dataSource addDataArray:userViewData];
    self.tableView.hidden = NO;
    self.emptyView.hidden = YES;
    [self.tableView reloadData];
}

//数据是空的时候显示
-(void)setEmptyUsers {
    self.emptyView.hidden = NO;
    self.tableView.hidden = YES;
    self.indicator.hidden = YES;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.tableFooterView = [UIView new];
        _tableView.backgroundColor = [UIColor whiteColor];
        [_tableView registerClass:[TableViewCell class] forCellReuseIdentifier:reuseId];
    }
    return _tableView;
}

//#pragma mark - 数据源方法
//-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    return self.userViewDatas.count;
//}
//
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    static NSString * cellID=@"cellID";
//    UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:cellID];
//    if (cell==nil) {
//        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID];
//    }
//    User *userViewData = self.userViewDatas[indexPath.row];
//    cell.textLabel.text = userViewData.name;
//    cell.detailTextLabel.text = userViewData.age;
//    return cell;
//}
@end
